/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <gtest/gtest.h>
#include <thread>

#include "common_utils.h"
#include <securec.h>
#include <sstream>
#include "loghelper.h"
#include "sha256_utils.h"
#include "se_hisysevent.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE;

enum TestParameter {
    TEST_INDEX_DATA_1 = 0,
    TEST_INDEX_DATA_2 = 1,
    TEST_INDEX_DATA_3 = 2,
    TEST_HEX_DATA_4 = 0x00,
    TEST_HEX_DATA_5 = 0xff
};

class CommonUtilsTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void CommonUtilsTest::SetUpTestCase()
{
    std::cout << " SetUpTestCase CommonUtilsTest." << std::endl;
}

void CommonUtilsTest::TearDownTestCase()
{
    std::cout << " TearDownTestCase CommonUtilsTest." << std::endl;
}

void CommonUtilsTest::SetUp()
{
    std::cout << " SetUp CommonUtilsTest." << std::endl;
}

void CommonUtilsTest::TearDown()
{
    std::cout << " TearDown CommonUtilsTest." << std::endl;
}

/**
 * @tc.name: BytesArrayToHexString_Test001
 * @tc.desc: Test CommonUtils BytesArrayToHexString.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, BytesArrayToHexString_Test001, TestSize.Level1)
{
    const uint8_t src[HEX_BYTE_LEN] = {};
    uint32_t length = 0;
    std::string hexString = CommonUtils::BytesArrayToHexString(src, length);
    ASSERT_TRUE(hexString == "");
}
/**
 * @tc.name: BytesArrayToHexString_Test002
 * @tc.desc: Test CommonUtils BytesArrayToHexString.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, BytesArrayToHexString_Test002, TestSize.Level1)
{
    const uint8_t src[HEX_BYTE_LEN] = {};
    uint32_t length = HEX_BYTE_LEN;
    std::string hexString = CommonUtils::BytesArrayToHexString(src, length);
    ASSERT_TRUE(hexString == "0000");
}
/**
 * @tc.name: HexStringToBytesArray_Test001
 * @tc.desc: Test CommonUtils HexStringToBytesArray.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, HexStringToBytesArray_Test001, TestSize.Level1)
{
    const std::string src = "";
    std::vector<uint8_t> bytes;
    CommonUtils::HexStringToBytesArray(src, bytes);
    ASSERT_TRUE(bytes.empty() == true);
}
/**
 * @tc.name: HexStringToBytesArray_Test002
 * @tc.desc: Test CommonUtils HexStringToBytesArray.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, HexStringToBytesArray_Test002, TestSize.Level1)
{
    const std::string src = "src";
    std::vector<uint8_t> bytes;
    CommonUtils::HexStringToBytesArray(src, bytes);
    ASSERT_TRUE(bytes.empty() == true);
}
/**
 * @tc.name: HexStringToBytesArray_Test003
 * @tc.desc: Test CommonUtils HexStringToBytesArray.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, HexStringToBytesArray_Test003, TestSize.Level1)
{
    const std::string src = "0123456789";
    std::vector<uint8_t> bytes = {TEST_HEX_DATA_5};
    CommonUtils::HexStringToBytesArray(src, bytes);
    ASSERT_TRUE(bytes.empty() == false);
}
/**
 * @tc.name: UnsignedCharToHexString_Test001
 * @tc.desc: Test CommonUtils UnsignedCharToHexString.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, UnsignedCharToHexString_Test001, TestSize.Level1)
{
    const uint8_t src = TEST_HEX_DATA_4;
    std::string hexString = CommonUtils::UnsignedCharToHexString(src);
    ASSERT_TRUE(hexString == "00");
}
/**
 * @tc.name: GetHexStringLength_Test001
 * @tc.desc: Test CommonUtils GetHexStringLength.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStringLength_Test001, TestSize.Level1)
{
    const std::string src = "";
    uint32_t hexStringLength = CommonUtils::GetHexStringLength(src);
    ASSERT_TRUE(hexStringLength == 0);
}
/**
 * @tc.name: GetHexStringLength_Test002
 * @tc.desc: Test CommonUtils GetHexStringLength.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStringLength_Test002, TestSize.Level1)
{
    const std::string src = "src";
    uint32_t hexStringLength = CommonUtils::GetHexStringLength(src);
    ASSERT_TRUE(hexStringLength == 1);
}
/**
 * @tc.name: GetHexStrByte_Test001
 * @tc.desc: Test CommonUtils GetHexStrByte.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStrByte_Test001, TestSize.Level1)
{
    const std::string src = "";
    uint32_t index = TEST_INDEX_DATA_1;
    uint8_t hexStrByte = CommonUtils::GetHexStrByte(src, index);
    ASSERT_TRUE(hexStrByte == 0);
}
/**
 * @tc.name: GetHexStrByte_Test002
 * @tc.desc: Test CommonUtils GetHexStrByte.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStrByte_Test002, TestSize.Level1)
{
    const std::string src = "src";
    uint32_t index = TEST_INDEX_DATA_3;
    uint8_t hexStrByte = CommonUtils::GetHexStrByte(src, index);
    ASSERT_TRUE(hexStrByte == 0);
}
/**
 * @tc.name: GetHexStrByte_Test003
 * @tc.desc: Test CommonUtils GetHexStrByte.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStrByte_Test003, TestSize.Level1)
{
    const std::string src = "src";
    uint32_t index = TEST_INDEX_DATA_1;
    uint8_t hexStrByte = CommonUtils::GetHexStrByte(src, index);
    ASSERT_TRUE(hexStrByte == 0);
}
/**
 * @tc.name: GetHexStrByte_Test004
 * @tc.desc: Test CommonUtils GetHexStrByte.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetHexStrByte_Test004, TestSize.Level1)
{
    const std::string src = "src";
    uint32_t index = TEST_INDEX_DATA_2;
    uint8_t hexStrByte = CommonUtils::GetHexStrByte(src, index);
    ASSERT_TRUE(hexStrByte != 0);
}
/**
 * @tc.name: GetApduRespSw1_Test001
 * @tc.desc: Test CommonUtils GetApduRespSw1.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw1_Test001, TestSize.Level1)
{
    const std::string apduResp = "";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw1(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_5);
}
/**
 * @tc.name: GetApduRespSw1_Test002
 * @tc.desc: Test CommonUtils GetApduRespSw1.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw1_Test002, TestSize.Level1)
{
    const std::string apduResp = "apduResp";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw1(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_4);
}
/**
 * @tc.name: GetApduRespSw1_Test003
 * @tc.desc: Test CommonUtils GetApduRespSw1.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw1_Test003, TestSize.Level1)
{
    const std::string apduResp = "0";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw1(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_5);
}
/**
 * @tc.name: GetApduRespSw2_Test001
 * @tc.desc: Test CommonUtils GetApduRespSw2.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw2_Test001, TestSize.Level1)
{
    const std::string apduResp = "";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw2(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_5);
}
/**
 * @tc.name: GetApduRespSw2_Test002
 * @tc.desc: Test CommonUtils GetApduRespSw2.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw2_Test002, TestSize.Level1)
{
    const std::string apduResp = "apduResp";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw2(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_4);
}
/**
 * @tc.name: GetApduRespSw2_Test003
 * @tc.desc: Test CommonUtils GetApduRespSw2.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetApduRespSw2_Test003, TestSize.Level1)
{
    const std::string apduResp = "0";
    uint8_t apduRespSw1 = CommonUtils::GetApduRespSw2(apduResp);
    ASSERT_TRUE(apduRespSw1 == TEST_HEX_DATA_5);
}
/**
 * @tc.name: IsApduRespSuccess_Test001
 * @tc.desc: Test CommonUtils IsApduRespSuccess.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, IsApduRespSuccess_Test001, TestSize.Level1)
{
    const std::string apduResp = "";
    bool apduRespSuccess = CommonUtils::IsApduRespSuccess(apduResp);
    ASSERT_TRUE(!apduRespSuccess);
}
/**
 * @tc.name: IsApduRespSuccess_Test002
 * @tc.desc: Test CommonUtils IsApduRespSuccess.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, IsApduRespSuccess_Test002, TestSize.Level1)
{
    const std::string apduResp = "apduResp";
    bool apduRespSuccess = CommonUtils::IsApduRespSuccess(apduResp);
    ASSERT_TRUE(!apduRespSuccess);
}

/**
 * @tc.name: CalculateHashCode_Test001
 * @tc.desc: Test CommonUtils CalculateHashCode.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, CalculateHashCode_Test001, TestSize.Level1)
{
    std::string inputStr = "6918661234931012134";
    std::string inputSha256 = "2711B0B459574B5AEEFE3ACDBD1ACCB026142115EA942F089C78C2C82BBCE7B5";
    std::string result = Sha256Utils::CalculateHashCode(inputStr);
    ASSERT_TRUE(result == inputSha256);
}

/**
 * @tc.name: HisysEvent_Test001
 * @tc.desc: Test CommonUtils HisysEvent_Test001.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, HisysEvent_Test001, TestSize.Level1)
{
    std::string cmd = "85E4";
    std::string rsp = "6A88";
    SeHisysEvent::WriteSeResponseHiSysEvent(cmd, rsp);
    ASSERT_TRUE(cmd.size() != 0);
}

/**
 * @tc.name: GetLocalTimeStampWithDate_Test001
 * @tc.desc: Test CommonUtils GetLocalTimeStampWithDate_Test001.
 * @tc.type: FUNC
 */
HWTEST_F(CommonUtilsTest, GetLocalTimeStampWithDate_Test001, TestSize.Level1)
{
    std::string timeStr = CommonUtils::GetLocalTimeStampWithDate();
    ASSERT_TRUE(timeStr.length() > 0);
}
}
}
}
